/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service.impl;

import com.ivanbiz.interfaceDAO.ApproveItemDAO;
import com.ivanbiz.interfaceDAO.CabangDAO;
import com.ivanbiz.interfaceDAO.GudangDAO;
import com.ivanbiz.interfaceDAO.GudangItemDAO;
import com.ivanbiz.interfaceDAO.InternalDeliveryDAO;
import com.ivanbiz.interfaceDAO.InternalDeliveryItemDAO;
import com.ivanbiz.interfaceDAO.ItemDAO;
import com.ivanbiz.model.ApproveItem;
import com.ivanbiz.model.Cabang;
import com.ivanbiz.model.GudangItem;
import com.ivanbiz.model.InternalDelivery;
import com.ivanbiz.model.InternalDeliveryItem;
import com.ivanbiz.model.Item;
import com.ivanbiz.service.GlobalService;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author City_Z
 */
public class GlobalServiceImpl implements GlobalService {

    private CabangDAO cabangDAO;
    private ApproveItemDAO approveItemDAO;
    private GudangDAO gudangDAO;
    private GudangItemDAO gudangItemDAO;
    private InternalDeliveryDAO internalDeliveryDAO;
    private InternalDeliveryItemDAO internalDeliveryItemDAO;
    private ItemDAO itemDAO;
    private static Map map = null;
    private static Map cabang1 = null;

    /**
     * @param cabangDAO the cabangDAO to set
     */
    public void setCabangDAO(CabangDAO cabangDAO) {
        this.cabangDAO = cabangDAO;
    }

    /**
     * @param approveItemDAO the approveItemDAO to set
     */
    public void setApproveItemDAO(ApproveItemDAO approveItemDAO) {
        this.approveItemDAO = approveItemDAO;
    }

    /**
     * @param gudangDAO the gudangDAO to set
     */
    public void setGudangDAO(GudangDAO gudangDAO) {
        this.gudangDAO = gudangDAO;
    }

    /**
     * @param gudangItemDAO the gudangItemDAO to set
     */
    public void setGudangItemDAO(GudangItemDAO gudangItemDAO) {
        this.gudangItemDAO = gudangItemDAO;
    }

    /**
     * @param internalDeliveryDAO the internalDeliveryDAO to set
     */
    public void setInternalDeliveryDAO(InternalDeliveryDAO internalDeliveryDAO) {
        this.internalDeliveryDAO = internalDeliveryDAO;
    }

    /**
     * @param internalDeliveryItemDAO the internalDeliveryItemDAO to set
     */
    public void setInternalDeliveryItemDAO(InternalDeliveryItemDAO internalDeliveryItemDAO) {
        this.internalDeliveryItemDAO = internalDeliveryItemDAO;
    }

    /**
     * @param itemDAO the itemDAO to set
     */
    public void setItemDAO(ItemDAO itemDAO) {
        this.itemDAO = itemDAO;
    }

    public String sendInternalDelivery(String noInternalDelivery, String kodeCabang) throws Exception {
        loadInitialCabang();
        String status = "";
        try {
            ApproveItemDAO appItemDAO = getConnectionCabangApproveItem(kodeCabang);
            List listInternalDeliveryItem = this.internalDeliveryItemDAO.getDataByEquals(InternalDeliveryItem.class, "nomorInternalDelivery", noInternalDelivery);
            for (int x = 0; x < listInternalDeliveryItem.size(); x++) {
                InternalDeliveryItem idi = (InternalDeliveryItem) listInternalDeliveryItem.get(x);
                ApproveItem approveItem = new ApproveItem();
                approveItem.setKodeBarcode(idi.getKodeBarcode());
                approveItem.setNamaItem(idi.getNamaItem());
                approveItem.setHargaModal(idi.getHargaModal());
                approveItem.setQuantity(idi.getQuantity());
                approveItem.setDateReceive(new Date());
                appItemDAO.save(approveItem);
                // this.approveItemDAO.save(approveItem);
                GudangItem gudangItem = (GudangItem) this.gudangItemDAO.getDataByEqual(GudangItem.class, "kodeBarcode", approveItem.getKodeBarcode());
                gudangItem.setStock(gudangItem.getStock() - idi.getQuantity());
                this.gudangItemDAO.update(gudangItem);
            }
            InternalDelivery internalDelivery = (InternalDelivery) this.internalDeliveryDAO.getDataByEqual(InternalDelivery.class, "nomorInternalDelivery", noInternalDelivery);
            internalDelivery.setStatus("Y");
            Cabang cabang = (Cabang) this.cabangDAO.getDataByEqual(Cabang.class, "kode", kodeCabang);
            internalDelivery.setCabang(cabang);
            this.internalDeliveryDAO.update(internalDelivery);
            status = "Y";
        } catch (Exception e) {
            status = "N";
            throw e;
        }
        return status;
    }

    public ApproveItemDAO getConnectionCabangApproveItem(String kodeCabang) {
        Map mapCabang1 = (HashMap) map.get(kodeCabang);
        ApproveItemDAO appItemDAO = (ApproveItemDAO) mapCabang1.get("approveItemDAO");
        return appItemDAO;
    }

    public ItemDAO getConnectionCabangItem(String kodeCabang) {
        Map mapCabang1 = (HashMap) map.get(kodeCabang);
        ItemDAO itemDAO = (ItemDAO) mapCabang1.get("itemDAO");
        return itemDAO;
    }

    public void loadInitialCabang() {
        if (cabang1 == null) {
            cabang1 = new HashMap();
            cabang1.put("approveItemDAO", this.approveItemDAO);
            cabang1.put("itemDAO", this.itemDAO);
        }
        if (map == null) {
            map = new HashMap();
            map.put("001", cabang1);
        }
    }

    public Item getInfoItem(String kodeBarcode, String kodeCabang) throws Exception {
        loadInitialCabang();
        ItemDAO itemDAOCabang = getConnectionCabangItem(kodeCabang);
        Item item = (Item) itemDAOCabang.getDataByEqual(Item.class, "kodeBarcode", kodeBarcode);
        return item;
    }

    @Override
    public String updateHargaItem(String kodeBarcode, double hargaJual1, double hargaJual2, String kodeCabang) throws Exception {
        loadInitialCabang();
        ItemDAO itemDAOCabang = getConnectionCabangItem(kodeCabang);
        Item item = (Item) itemDAOCabang.getDataByEqual(Item.class, "kodeBarcode", kodeBarcode);
        item.setHargaJual(hargaJual1);
        item.setHargaJual2(hargaJual2);
        itemDAOCabang.update(item);
        return "Y";
    }

    @Override
    public List searchByLimit(long limit,String kodeCabang) throws Exception {
        loadInitialCabang();
        ItemDAO itemDAOCabang = getConnectionCabangItem(kodeCabang);
        return itemDAOCabang.searchByLimit(limit); 
       // List list = (List)this.itemDAO.g
    }
}
